package com.example.imtoken.modules.main.wallet.details.trade.code

import android.text.InputType
import android.view.View
import com.blankj.utilcode.constant.RegexConstants
import com.blankj.utilcode.util.*
import com.example.imtoken.R
import com.example.imtoken.core.App
import com.example.imtoken.core.OnMultifunctionalSelectorListener
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.core.utils.MakeQRCodeUtil
import com.example.imtoken.databinding.ActivityReceivingCodeBinding
import com.example.imtoken.room.schema.WalletAddressSchema
import com.example.imtoken.view.function.MultifunctionalSelectorDialogFragment
import com.example.imtoken.view.function.data.onFunction2
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

/**
 * 收款码
 */
class ReceivingCodeActivity :
    BaseActivity<ReceivingCodeViewModel, ActivityReceivingCodeBinding>(ReceivingCodeViewModel::class.java) {

    private val address by lazy {
        intent.getStringExtra("address")
    }

    private val title by lazy {
        intent.getStringExtra("title")?.toUpperCase()
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_receiving_code
    }

    override fun init() {
        BusUtils.register(this)
        initTitle()
        initClick()
        initObserve()
        if (address != null) createQRCode(address)
        dataBinding.tvCodeTips.text =
            getString(R.string.scanCodeTips, "", title)
        dataBinding.address = App.createLiveData.value?.coin?.address
    }

    private fun initObserve() {
        viewModel.getWalletDetailsLiveData().observe(this) {
            val content = viewModel.createQRContent(it.fatherAddress, it.address)
            createQRCode(content)
        }
    }

    @BusUtils.Bus(tag = BusConfig.out_data_sticky_receiving_code_activity, sticky = true)
    fun getWalletDetails(walletAddressSchema: WalletAddressSchema) {
        viewModel.setWalletDetailsLiveData(walletAddressSchema)
    }

    private fun createQRCode(content: String?) {
        val bitmap = MakeQRCodeUtil.makeQRImage(
            content,
            ConvertUtils.sp2px(210f),
            ConvertUtils.sp2px(210f)
        )
        dataBinding.ivCode.setImageBitmap(bitmap)
    }

    private fun initClick() {
        dataBinding.include7.ivHeadBack.setOnClickListener { this.finish() }
        dataBinding.tvAddress.setOnClickListener {
            ClipboardUtils.copyText(App.createLiveData.value?.coin?.address)
            ToastUtils.showShort(R.string.copyTips)
        }
        dataBinding.llCopy.setOnClickListener {
            ClipboardUtils.copyText(App.createLiveData.value?.coin?.address)
            ToastUtils.showShort(R.string.copyTips)
        }
        dataBinding.llFunction.setOnClickListener {
            //更多功能
            val dialog = MultifunctionalSelectorDialogFragment()
            dialog.setData(onFunction2(), object : OnMultifunctionalSelectorListener {
                override fun selectPosition(position: Int) {
                    when (position) {
                        0 -> {
                            //收款金额设置
                            MaterialDialog.Builder(this@ReceivingCodeActivity)
                                .title(R.string.amountReceived)
                                .inputType(InputType.TYPE_CLASS_TEXT)
                                .input(
                                    R.string.inputAmountReceivedTips,
                                    0,
                                    false
                                ) { _, input ->
                                    if (RegexUtils.isMatch(
                                            RegexConstants.REGEX_POSITIVE_FLOAT,
                                            input
                                        ) || RegexUtils.isMatch(
                                            RegexConstants.REGEX_POSITIVE_INTEGER,
                                            input
                                        )
                                    ) {
                                        dataBinding.tvCodeTips.text = getString(
                                            R.string.scanCodeTips,
                                            input.toString(),
                                            title
                                        )
                                        val content = if (address != null) {
                                            viewModel.createQRContent(
                                                address,
                                                address,
                                                input.toString()
                                            )
                                        } else {
                                            viewModel.createQRContent(
                                                viewModel.getWalletDetailsLiveData().value?.fatherAddress,
                                                viewModel.getWalletDetailsLiveData().value?.address,
                                                input.toString()
                                            )

                                        }
                                        createQRCode(content)
                                    } else {
                                        ToastUtils.showShort(R.string.inputAmountErrorTips)
                                    }
                                }
                                .positiveText(R.string.confirm)
                                .negativeText(R.string.cancel)
                                .cancelable(false)
                                .show()
                        }
                    }
                }

            })
            dialog.show(supportFragmentManager, "dialog")
        }
    }

    private fun initTitle() {
        dataBinding.include7.tvHeadTitles.text = getString(R.string.receiving)
        dataBinding.include7.vLine.visibility = View.INVISIBLE

        when (title?.toLowerCase()) {
            "eth","usdt" -> {
                initBar(true, R.color.welcomeBgColor)
                dataBinding.include7.clHeadHome.setBackgroundColor(ColorUtils.getColor(R.color.welcomeBgColor))
                dataBinding.clReceivingHome.setBackgroundResource(R.color.welcomeBgColor)

                dataBinding.vLiner.visibility = View.VISIBLE
                dataBinding.llFunction.visibility = View.VISIBLE
            }
            "trx","usdj" ->{
                initBar(true, R.color.xui_config_color_red)
                dataBinding.include7.clHeadHome.setBackgroundColor(ColorUtils.getColor(R.color.xui_config_color_red))
                dataBinding.clReceivingHome.setBackgroundResource(R.color.xui_config_color_red)

                dataBinding.vLiner.visibility = View.GONE
                dataBinding.llFunction.visibility = View.GONE
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.out_data_sticky_receiving_code_activity)
        BusUtils.unregister(this)
    }
}